/**
 * MCP (Model Context Protocol) Standard Type Definitions
 * 适配低代码平台的虚拟文件系统
 */

// ===== 基础类型 =====

export interface MCPError {
  code: number;
  message: string;
  data?: any;
}

export interface MCPResponse<T> {
  success: boolean;
  data?: T;
  error?: MCPError;
  _meta?: {
    requestId: string;
    timestamp: number;
    duration?: number;
  };
}

// ===== 资源管理 =====

export interface MCPResource {
  uri: string;
  name: string;
  description?: string;
  mimeType: string;
  metadata: {
    projectId: string;
    entityType: 'document' | 'type' | 'folder' | 'file';
    version?: string;
    lastModified: number;
    size?: number;
    hash?: string;
    [key: string]: any;
  };
}

export interface MCPResourceContent {
  uri: string;
  mimeType?: string;
  text?: string;
  blob?: string; // base64 encoded
}

export interface MCPResourceTemplate {
  uriTemplate: string;
  name: string;
  description?: string;
  mimeType?: string;
}

// ===== 工具调用 =====

export interface MCPTool {
  name: string;
  description: string;
  inputSchema: {
    type: "object";
    properties: Record<string, any>;
    required?: string[];
  };
}

export interface MCPToolCall {
  name: string;
  arguments?: Record<string, any>;
}

export interface MCPToolResult {
  content: Array<{
    type: "text" | "image" | "resource";
    text?: string;
    data?: string;
    id?: string;
  }>;
  isError?: boolean;
}

// ===== 提示模板 =====

export interface MCPPrompt {
  name: string;
  description?: string;
  arguments?: Array<{
    name: string;
    description?: string;
    required?: boolean;
  }>;
}

export interface MCPPromptMessage {
  role: "user" | "assistant" | "system";
  content: {
    type: "text" | "image" | "resource";
    text?: string;
    data?: string;
    id?: string;
  };
}

export interface MCPPromptResult {
  description: string;
  messages: MCPPromptMessage[];
}

// ===== 消息类型 =====

export interface MCPMessage {
  id: string;
  method?: string;
  params?: any;
  result?: any;
  error?: MCPError;
  jsonrpc: "2.0";
}

// ===== 服务器能力 =====

export interface MCPServerCapabilities {
  resources?: {
    subscribe?: boolean;
    list?: boolean;
    read?: boolean;
  };
  tools?: {
    list?: boolean;
    call?: boolean;
  };
  prompts?: {
    list?: boolean;
    get?: boolean;
  };
  logging?: {
    level?: "debug" | "info" | "notice" | "warning" | "error" | "critical" | "alert" | "emergency";
  };
}

// ===== 客户端能力 =====

export interface MCPClientCapabilities {
  experimental?: Record<string, any>;
  sampling?: {
    [key: string]: any;
  };
}

// ===== 初始化信息 =====

export interface MCPClientInfo {
  name: string;
  version: string;
}

export interface MCPServerInfo {
  name: string;
  version: string;
}

export interface MCPInitializeParams {
  protocolVersion: string;
  capabilities: MCPClientCapabilities;
  clientInfo: MCPClientInfo;
}

export interface MCPInitializeResult {
  protocolVersion: string;
  capabilities: MCPServerCapabilities;
  serverInfo: MCPServerInfo;
}

// ===== 低代码平台特定类型 =====

export interface ProjectContext {
  projectId: string;
  projectName: string;
  userId?: number;
  userName?: string;
  permissions: string[];
  environment: 'development' | 'production' | 'staging';
}

export interface FileOperation {
  projectId: string;
  path: string;
  operation: 'read' | 'write' | 'delete' | 'move' | 'copy';
  content?: string;
  options?: {
    skipValidation?: boolean;
    writeMode?: 'code' | 'vi';
    author?: string;
    message?: string;
    createIfMissing?: boolean;
    overwrite?: boolean;
  };
}

export interface BatchFileOperation {
  projectId: string;
  operations: FileOperation[];
  options?: {
    parallel?: boolean;
    continueOnError?: boolean;
  };
}

// ===== URI 解析 =====

export interface ParsedURI {
  protocol: string;
  projectId: string;
  path: string;
  query?: Record<string, string>;
  fragment?: string;
}

// 错误代码常量
export const MCP_ERROR_CODES = {
  INVALID_REQUEST: -32600,
  METHOD_NOT_FOUND: -32601,
  INVALID_PARAMS: -32602,
  INTERNAL_ERROR: -32603,
  PARSE_ERROR: -32700,
  RESOURCE_NOT_FOUND: -32001,
  RESOURCE_ACCESS_DENIED: -32002,
  RESOURCE_ALREADY_EXISTS: -32003,
  TOOL_EXECUTION_ERROR: -32004,
  VALIDATION_ERROR: -32005,
  PROJECT_ACCESS_DENIED: -32006,
  INSUFFICIENT_PERMISSIONS: -32007,
} as const;

// 工具类型
export type MCPErrorCode = typeof MCP_ERROR_CODES[keyof typeof MCP_ERROR_CODES];